package com.zt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 *
 */
@SpringBootApplication
@RestController
public class Application {

    private static Random random = new Random();
    private int[] code = new int[]{200};

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @GetMapping("/getUser")
    public void get(HttpServletResponse response) throws Exception {
        Thread.sleep((int) random.nextFloat() * 1000);
        response.getWriter().write("OK");
        response.setStatus(code[((int) Thread.currentThread().getId()) % code.length]);
    }

    @PutMapping("/addUser")
    public void add(HttpServletResponse response) throws Exception {
        Thread.sleep((int) random.nextFloat() * 1000);
        response.getWriter().write("OK");
        response.setStatus(code[((int) Thread.currentThread().getId()) % code.length]);
    }

    @PostMapping("/updateUser")
    public void update(HttpServletResponse response) throws Exception {
        Thread.sleep((int) random.nextFloat() * 1000);
        response.getWriter().write("OK");
        response.setStatus(code[((int) Thread.currentThread().getId()) % code.length]);
    }

    @DeleteMapping("/deleteUser")
    public void delete(HttpServletResponse response) throws Exception {
        Thread.sleep((int) random.nextFloat() * 1000);
        response.getWriter().write("OK");
        response.setStatus(code[((int) Thread.currentThread().getId()) % code.length]);
    }
}
